import * as paths from '@/constants/Paths';
import { merge } from 'lodash';
import { SIDER_MENU } from '@/constants/ModelNamespaces';

//Menu的顶级父菜单SubMenu的key数组
const rootSubmenuKeys = ['sys', `${paths.STORE}`, 'business'];

export default {
    namespace:SIDER_MENU,
    state: {
        selectedKeys: [],
        defaultOpenKeys:[`${paths.PUTAWAY}`], //默认展开商城
        openKeys: [],
    },
    reducers:{
        updateSelectedKeys(state, {payload}){
            const temp = merge({}, state);
            temp.selectedKeys = payload.selectedKeys;
            return temp;
        },
        updateOpenKeys(state, {payload}){
            const temp = merge({}, state);
            //只展开当前打开的父菜单.
            //1.从openKeys中找出刚打开的菜单key.
            const latestOpenKey = payload.openKeys.find(key => temp.openKeys.indexOf(key) === -1);
            //2.如果不是顶级父菜单
            if(rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
                temp.openKeys = payload.openKeys;
            }else{
                //3.如果是顶级父菜单,则openKeys中只保留最新的父菜单.
                temp.openKeys = latestOpenKey ? [latestOpenKey] : [];
            }

            return temp;
        },
    }
}




